package krow.dev.contest;

import java.util.List;

import krow.dev.contest.Constants.Invoke;
import krow.dev.contest.controller.FavoriteListFactory;
import krow.dev.contest.controller.RssReader;
import krow.dev.contest.model.ContestItem;
import krow.dev.contest.model.FavoriteItem;
import krow.dev.contest.view.ContestListAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class ContestListActivity extends Activity implements OnClickListener{
	
	private RssReader mRssReaderTask;
	private ContestListAdapter mDetailcategoryListAdapter;
	private ListView mDetailcategoryListView;
	private TextView mCountView;
	
	
	private String mTitle;
	private String mURL;
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contestlist);

		Intent intent = getIntent();
		if (intent != null) {
			mTitle = intent.getStringExtra(Invoke.KEY_TITLE);
			mURL = intent.getStringExtra(Invoke.KEY_URL);
		}
		
		initializeView();
		System.out.println("이니셜라이즈뷰");
		requestContest(mURL);
	}
	
	
	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.layout_back:
			finish();
			break;
		}
	}
	
	private void initializeView(){
		
		TextView titleView = (TextView) findViewById(R.id.text_title);
		titleView.setText(mTitle);
		
		mCountView = (TextView)findViewById(R.id.text_count);
		
		mDetailcategoryListAdapter = new ContestListAdapter(getApplicationContext());
		mDetailcategoryListView = (ListView) findViewById(R.id.list_menu);
		mDetailcategoryListView.addFooterView(getLayoutInflater().inflate(R.layout.footer_dummy, null));
		mDetailcategoryListView.setOnItemClickListener(mDetailItemClickListener);
		mDetailcategoryListView.setAdapter(mDetailcategoryListAdapter);
		
		findViewById(R.id.layout_back).setOnClickListener(this);
	}
	
	@Override
	protected void onStop() {
		if(mRssReaderTask != null){
			mRssReaderTask.cancel();
		}
		super.onStop();
	}
	
	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
	}

	
	private void requestContest(String url){
		Log.e("test", "url: " + url);
		
		mRssReaderTask = new RssReader(this);
		mRssReaderTask.setCompletionListener(mBuildCompletionListener);
		mRssReaderTask.setDetailcategoryItemURL(url);
		mRssReaderTask.execute();
		
	}
	
	private RssReader.OnCompletionListener mBuildCompletionListener = new RssReader.OnCompletionListener() {
		
		@Override
		public void onCompletion(List<ContestItem> detailcategoryItemList) {
			
			mDetailcategoryListAdapter.setDetailcategoryItemList(detailcategoryItemList);
			mDetailcategoryListAdapter.notifyDataSetChanged();
			 
			mCountView.setText("+" + mRssReaderTask.getContestSize());
		}
	};
	
	private AdapterView.OnItemClickListener mDetailItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if (view.getTag() == null) {
				return;
			}
			
			ContestItem contestItem = (ContestItem) view.getTag();
			
			Intent intent = new Intent(ContestListActivity.this, ContestDetailActivity.class);
			intent.putExtra(Invoke.KEY_TITLE, contestItem.getTitle());
			intent.putExtra(Invoke.KEY_CREATOR, contestItem.getCreator());
			intent.putExtra(Invoke.KEY_DEPARTMENT, contestItem.getDepartment());
			intent.putExtra(Invoke.KEY_DESCRIPTION, contestItem.getDescription());
			intent.putExtra(Invoke.KEY_URL, contestItem.getLink());
			
			startActivity(intent);
			overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
		}
	};
}
